# import logging
#
# from flask import Blueprint, request, jsonify
#
# # from api.service.query_service import get_impact_encoding_attributes, build_duplicate_query
# from models import db
#
# app = Blueprint('query', __name__)
# logger = logging.getLogger(__name__)
#
#
# # 查重接口
# @app.route('/materials/check-duplicates', methods=['POST'])
# def check_duplicate():
#     try:
#         data = request.get_json()
#         if not data or 'data' not in data:
#             return jsonify({
#                 "meta": {"code": 400, "message": "Invalid request format"},
#                 "data": None
#             }), 400
#
#         input_list = data['data']
#         results = []
#
#         try:
#             for item in input_list:
#                 # 提取字段
#                 category_tree_id = item.get('mater_cate')  # 必须是 BIGINT
#                 if not category_tree_id:
#                     results.append({
#                         "mater_cate": None,
#                         "duplicate_materials": [],
#                         "error": "Missing 'mater_cate'"
#                     })
#                     continue
#
#                 # 动态提取所有属性值（除 mater_cate 外）
#                 input_values = {k: v for k, v in item.items() if k != 'mater_cate'}
#
#                 # 获取该分类下所有影响赋码的属性
#                 impact_attrs = get_impact_encoding_attributes(category_tree_id)
#                 if not impact_attrs:
#                     results.append({
#                         "mater_cate": category_tree_id,
#                         "is_duplicate": False,
#                         "input_fact": item,
#                         "duplicate_materials": [],
#                         "warning": "No impact_encoding attributes found for this category"
#                     })
#                     continue
#
#                 # 构建查重查询
#                 duplicates = build_duplicate_query(impact_attrs, input_values)
#
#                 # 构建响应
#                 dup_mats = []
#                 for dup in duplicates:
#                     bi = dup["basic_info"]
#                     matched_attrs = [
#                         {"attribute_name": attr["attr_name_en"], "attribute_value": input_values[attr["attr_name_en"]]}
#                         for attr in impact_attrs if attr["attr_name_en"] in input_values
#                     ]
#                     dup_mats.append({
#                         "material_code": bi.material_code,
#                         "material_name": bi.material_name,
#                         "drawing_model": bi.model_number or bi.drawing_number,
#                         "material_description": bi.material_description,
#                         "configuration_description": bi.configuration_description,
#                         "import_domestic": bi.import_domestic,
#                         "matched_attributes": matched_attrs
#                     })
#
#                 results.append({
#                     "mater_cate": category_tree_id,
#                     "is_duplicate": False if len(dup_mats) == 0 else True,
#                     "input_fact": item,
#                     "duplicate_materials": dup_mats
#                 })
#
#         finally:
#             db.session.close()
#
#         return jsonify({
#             "code": 200,
#             "message": "Success",
#             "requestId": data.get("meta").get("requestId"),
#             "data": results
#         })
#
#     except Exception as e:
#         logger.error(f"Error in /api/v1/materials/check-duplicates: {str(e)}")
#         return jsonify({
#             "code": 500,
#             "message": "Internal Server Error",
#             "data": None
#         }), 500
